package cn.wolfcode.plus.controller;

import cn.wolfcode.plus.domain.RedPacketInfo;
import cn.wolfcode.plus.service.IRedisService;
import cn.wolfcode.plus.service.RedPacketInfoService;
import cn.wolfcode.plus.service.RedPacketRecordService;
import cn.wolfcode.plus.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.LongAdder;

@RestController
@RequestMapping("redPackets")
public class RedPacketInfoController {

    // 存入DB中
    @Autowired
    private RedPacketInfoService redPacketInfoService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private RedPacketRecordService redPacketRecordService;

    private final LongAdder longAdder = new LongAdder();
    // 发红包
    @PostMapping()
    public JsonResult handout(RedPacketInfo redPacketInfo) {
        redPacketInfo.setRemainingAmount(redPacketInfo.getTotalAmount());
        redPacketInfo.setRemainingPacket(redPacketInfo.getTotalPacket());
        if (redPacketInfoService.save(redPacketInfo)) {
            // 此时id已有
            redisService.save(redPacketInfo);
            return JsonResult.success();
        }
        return JsonResult.error();
    }

    /*// 抢红包
    @PutMapping()
    public JsonResult snatch(RedPacketInfo redPacketInfo) {
        // 判断是否还有红包
        // 先去redis中查询
        try {
            int count = redisService.getRemainCount(redPacketInfo);
            // 9,9
            if (count == 0){
                return JsonResult.error("红包已经领完了!");
            }
            int amount = redisService.getRemainAmount(redPacketInfo);
            // 开始抢红包逻辑
            redPacketInfo.setRemainingPacket(count);
            redPacketInfo.setRemainingAmount(amount);
            int randomMoney = redPacketInfoService.snatch(redPacketInfo);
            // 红包数-1
            redisService.decr(redPacketInfo.getId(),randomMoney);
            // 更新快照表? 批量更新,抢完了再更新
            redPacketRecordService.save(redPacketInfo.getUid(),redPacketInfo.getId(),randomMoney);
            return JsonResult.success("抢到红包"+randomMoney);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("服务器异常");
        }
    }*/

    // 抢红包
    @PutMapping()
    public JsonResult snatch(RedPacketInfo redPacketInfo) {
        // 判断是否还有红包
        // 先去redis中查询
        try {
            int count = redisService.getRemainCount(redPacketInfo);

            // 9,9
            if (longAdder.longValue() <= 10){
                redisService.decrAmount(redPacketInfo.getId());
                int amount = redisService.getRemainAmount(redPacketInfo);
                // 开始抢红包逻辑
                redPacketInfo.setRemainingPacket(count);
                redPacketInfo.setRemainingAmount(amount);
                int randomMoney = redPacketInfoService.snatch(redPacketInfo);
                // 红包数-1
                redisService.decr(redPacketInfo.getId(),randomMoney);
                longAdder.increment();
                // 更新快照表? 批量更新,抢完了再更新
                redPacketRecordService.save(redPacketInfo.getUid(),redPacketInfo.getId(),randomMoney);
                return JsonResult.success("抢到红包"+randomMoney);
            }else {
                return JsonResult.error("红包已经领完了!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("服务器异常");
        }
    }
}
